package com.ruoyi.business.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.ruoyi.business.domain.Chain;
import com.ruoyi.business.domain.vo.ChainVO;
import com.ruoyi.business.mapper.ChainMapper;
import com.ruoyi.business.service.ChainService;
import org.springframework.stereotype.Service;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.utils.StringUtils;

import java.util.Map;

import static com.ruoyi.business.domain.table.ChainTableDef.CHAIN;


/**
 * 服务实现 : 链表资产
 * @Author  : ljq
 * @Created : 2025/10/14
 */
@Service
public class ChainServiceImpl   extends ServiceImpl<ChainMapper, Chain> implements ChainService {

    @Override
    public Page<ChainVO> listByCondition(Chain entity, PageDomain domain) {
        QueryChain<Chain> queryChain = QueryChain.of(this.getMapper())
                .select(CHAIN.DEFAULT_COLUMNS)
                .from(CHAIN).as("t1");
        this.baseCondition(queryChain, entity);
        return queryChain
                .withRelations()
                .pageAs(new Page<>(domain.getPageNum(), domain.getPageSize()), ChainVO.class);
    }


    /* ------------------- 基础查询条件 --------------------- */
    private QueryChain baseCondition(QueryChain chain, Chain entity){
        // 资产键
        if(StringUtils.isNotEmpty(entity.getAssetKey())){
            chain.where(CHAIN.ASSET_KEY.eq(entity.getAssetKey()));
        }
        // 交易号
        if(StringUtils.isNotEmpty(entity.getTxId())){
            chain.where(CHAIN.TX_ID.eq(entity.getTxId()));
        }
        // 业务ID
        if(StringUtils.isNotNull(entity.getBusinessId())){
            chain.where(CHAIN.BUSINESS_ID.eq(entity.getBusinessId()));
        }
        // 业务名称
        if(StringUtils.isNotEmpty(entity.getBusinessName())){
            chain.where(CHAIN.BUSINESS_NAME.eq(entity.getBusinessName()));
        }
        // 备注
        if(StringUtils.isNotEmpty(entity.getComment())){
            chain.where(CHAIN.COMMENT.eq(entity.getComment()));
        }
        // 附加内容
        if(StringUtils.isNotEmpty(entity.getOther())){
            chain.where(CHAIN.OTHER.eq(entity.getOther()));
        }
        // 状态
        if(StringUtils.isNotEmpty(entity.getStatus())){
            chain.where(CHAIN.STATUS.eq(entity.getStatus()));
        }
        // 创建者
        if(StringUtils.isNotEmpty(entity.getCreateBy())){
            chain.where(CHAIN.CREATE_BY.eq(entity.getCreateBy()));
        }
        // 更新者
        if(StringUtils.isNotEmpty(entity.getUpdateBy())){
            chain.where(CHAIN.UPDATE_BY.eq(entity.getUpdateBy()));
        }
        // 删除标志
        if(StringUtils.isNotNull(entity.getDelFlag())){
            chain.where(CHAIN.DEL_FLAG.eq(entity.getDelFlag()));
        }

        Map<String, Object> params = entity.getParams();

        // 创建时间
        String createTimeRange = params.getOrDefault("createTimeRange", "").toString();
        if (StringUtils.isNotEmpty(createTimeRange)) {
            String[] split = createTimeRange.split(",");
            chain.where(CHAIN.CREATE_TIME.between(split[0], split[1]));
        }

        // 更新时间
        String updateTimeRange = params.getOrDefault("updateTimeRange", "").toString();
        if (StringUtils.isNotEmpty(updateTimeRange)) {
            String[] split = updateTimeRange.split(",");
            chain.where(CHAIN.UPDATE_TIME.between(split[0], split[1]));
        }

        // 资产键-模糊匹配
        String assetKeyLike = params.getOrDefault("assetKeyLike", "").toString();
        if (StringUtils.isNotEmpty(assetKeyLike)) {
            chain.where(CHAIN.ASSET_KEY.like(assetKeyLike));
        }

        // 交易号-模糊匹配
        String txIdLike = params.getOrDefault("txIdLike", "").toString();
        if (StringUtils.isNotEmpty(txIdLike)) {
            chain.where(CHAIN.TX_ID.like(txIdLike));
        }

        // 业务名称-模糊匹配
        String businessNameLike = params.getOrDefault("businessNameLike", "").toString();
        if (StringUtils.isNotEmpty(businessNameLike)) {
            chain.where(CHAIN.BUSINESS_NAME.like(businessNameLike));
        }

        // 备注-模糊匹配
        String commentLike = params.getOrDefault("commentLike", "").toString();
        if (StringUtils.isNotEmpty(commentLike)) {
            chain.where(CHAIN.COMMENT.like(commentLike));
        }

        // 附加内容-模糊匹配
        String otherLike = params.getOrDefault("otherLike", "").toString();
        if (StringUtils.isNotEmpty(otherLike)) {
            chain.where(CHAIN.OTHER.like(otherLike));
        }

        // 状态-模糊匹配
        String statusLike = params.getOrDefault("statusLike", "").toString();
        if (StringUtils.isNotEmpty(statusLike)) {
            chain.where(CHAIN.STATUS.like(statusLike));
        }

       // 状态-下拉框多选
        String statusList = params.getOrDefault("statusList", "").toString();
        if (StringUtils.isNotEmpty(statusList)) {
            chain.where(CHAIN.STATUS.in(statusList.split(",")));
        }

        return chain;
    }
}